<?php
// $Id$

/**
 * @file
 * TODO
 */

function _portals_node_portal_pid_from_nid($nid) {
  $check_if_node_portal_exists = db_query(
   "SELECT pn.pid
    FROM {portals_nodes} pn
    WHERE pn.nid = %d"
    , $nid);
  $current_set = db_fetch_object($check_if_node_portal_exists);
  if (isset($current_set->pid)) {
    return (int)$current_set->pid;
  }
  else {
    return -1;
  }
}

function _portals_node_portal_portalid_from_name($pname) {
  $get_the_pid_from_nme = db_query(
   "SELECT p.pid
    FROM {portals} p
    WHERE p.nme = '%s'"
    , drupal_strtolower($pname));
  $pid_result_object = db_fetch_object($get_the_pid_from_nme);
  if (isset($pid_result_object->pid)) {
    return $pid_result_object->pid;
  }
  else {
    return -1;
  }
}

function _portals_node_get_nme_from_nid($nid) {
  $result = db_query(
   "SELECT p.nme
    FROM {portals} p
    INNER JOIN {portals_nodes} pn
    WHERE pn.nid = %d
    AND pn.pid = p.pid"
    , $nid);
  return db_fetch_object($result)->nme;
}

function _portals_get_cnf_from_pid($pid) {
  $result = db_query(
   "SELECT p.cnf
    FROM {portals} p
    WHERE p.pid = %d"
    , $pid);
  $returnobject = db_fetch_object($result);
  return (isset($returnobject->cnf) ? unserialize($returnobject->cnf) : FALSE);
}

function _portals_save_cnf_to_pid($nid, $cnf) {
  $result = db_query(
   "UPDATE {portals}
    SET cnf = '%s'
    WHERE pid = %d"
    , $cnf, $nid);
}

function _portals_create_portal($nme, $cnf) {
  $result = db_query(
   "INSERT INTO {portals}
    (nme, cnf)
    VALUES ('%s', '%s')"
    , drupal_strtolower($nme), $cnf);
  return db_last_insert_id('portals', 'pid');
}

function _portals_delete_portal($pid) {
  $result = db_query(
   "DELETE FROM {portals}
    WHERE pid=%d"
    , $pid);
}
